﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using Keepo.Helpers;

namespace Keepo.Designer.Controllers
{
    public class FacebookController : Controller
    {
        //
        // GET: /Facebook/

        public ActionResult Index(string code)
        {
            //create url to get AccessToken from facebook
            var facebookUrl = "https://graph.facebook.com/oauth/access_token?"
                                + "client_id=115226795207074&redirect_uri=" + StringHelper.SiteURL(Url) + "Facebook&"
                                + "client_secret=bcd5c7198aea0415f45effdd06a0925d&code=" + code;

            WebClient webClient = new WebClient();
            var responseText = webClient.DownloadString(facebookUrl);

            //cut the accessToken from the response text
            responseText = responseText.Replace("access_token=", "");
            string accessToken = responseText.Substring(0, responseText.IndexOf("&expires"));
            Session["accessToken"] = accessToken;

            return View();
        }

        public ActionResult GetAccessToken()
        {
            return Content(Session["accessToken"] as string);
        }

        public ActionResult SetAccessToken(string accessToken)
        {
            Session["accessToken"] = accessToken;
            return Content(Session["accessToken"] as string);
        }
    }
}
